package com.proyecto.track.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.proyecto.track.bo.Account;
import com.proyecto.track.bo.UserPerson;
import com.proyecto.track.dao.AccountDAOImp;
import com.proyecto.track.dao.UserPersonDAO;

@Transactional(rollbackFor=Exception.class)
public class UserPersonService{

	private UserPersonDAO userPersonDAO;

	private AccountDAOImp accountDAOImp;
	
	public Account searchAccount(String userName){
		return accountDAOImp.searchAccount(userName);
	}
	
	public Long createUserPerson(UserPerson userPerson){
		userPersonDAO.createUserPerson(userPerson);
		return userPerson.getId();
	}
	
	public Long deleteUserPerson(Long id){
		userPersonDAO.deleteUserPerson(userPersonDAO.searchUserPerson(id));
		return id;
	}
	
	public Long modifyUserPerson(UserPerson userPersonActualizada){
		UserPerson userPerson = userPersonDAO.searchUserPerson(userPersonActualizada.getId());
		BeanUtils.copyProperties(userPersonActualizada,userPerson);
		userPersonDAO.modifyUserPerson(userPerson);
		return userPerson.getId();
	}
	
	public List<UserPerson> searchUser(){
		return userPersonDAO.searchUser();
	}
	
	public List<UserPerson> searchUser(String text){
		return userPersonDAO.searchUser(text);
	}
	
	public UserPerson searchUserPerson(Long id){
		return userPersonDAO.searchUserPerson(id);
	}

	public void setUserPersonDAO(UserPersonDAO userPersonDAO) {
		this.userPersonDAO = userPersonDAO;
	}


	public UserPersonDAO getUserPersonDAO() {
		return userPersonDAO;
	}

	public AccountDAOImp getAccountDAOImp() {
		return accountDAOImp;
	}

	public void setAccountDAOImp(AccountDAOImp accountDAOImp) {
		this.accountDAOImp = accountDAOImp;
	}

	public List<UserPerson> getUsersFiltered(List<UserPerson> usersInsideProject) {
		List<UserPerson> usersFiltered = new ArrayList<UserPerson>();
		usersFiltered = this.searchUser();
		usersFiltered.removeAll(usersInsideProject);
		return usersFiltered;
	}

	public List<UserPerson> searchUsersPerson(Long[] idsUsersPersons) {
		List<UserPerson> usersPerson = new ArrayList<UserPerson>();
			if (idsUsersPersons!=null) {
				for (Long id : idsUsersPersons) {
					usersPerson.add(this.searchUserPerson(id));
				}
			}

		return usersPerson;
	}

	public UserPerson searchUserByAccount(Long accountId) {
		// TODO Auto-generated method stub
		return userPersonDAO.searchUserPersonByAccount(accountId);

	}

}
